package org.zeqp.chatAI.aiterface.animal;

import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONUtil;
import com.baidu.aip.imageclassify.AipImageClassify;
import org.json.JSONObject;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class AnimalUtils {

    public static HashMap<String,String> defaultOptions = new HashMap<>();

    public static final String  ACCESSKEY ="w9Ho0TR8g3frjjLnAVPhCoUa";

    public static final String  PRIVATEKEY ="byrS9VSVBcca9qKmcgX8SFLEjHrCvX9K";
    public static final String  APP_ID ="42169822";


    public static AipImageClassify getAipClient(){
        AipImageClassify client = new AipImageClassify(APP_ID, ACCESSKEY, PRIVATEKEY);
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        return client;
    }

    public static void animalCheck(byte[] imgBytes,Map<String,Object> options){
        if(options==null) {
            defaultOptions.put("top_num", "3");
            defaultOptions.put("baike_num", "5");
        }
        AipImageClassify aipClient = AnimalUtils.getAipClient();
        JSONObject jsonObject = aipClient.animalDetect(imgBytes,defaultOptions);
        ImgCheckResponse animalResult = JSONUtil.toBean(jsonObject.toString(), ImgCheckResponse.class);

        System.out.println(JSONUtil.toJsonStr(animalResult));
    }

    public static void main(String[] args) {
        ClassLoader classLoader = AnimalUtils.class.getClassLoader();
        InputStream resourceAsStream = classLoader.getResourceAsStream("1.png");
        //InputStream resourceAsStream = AnimalUtils.class.getClass().getClassLoader().getResourceAsStream("classpath:/1.png");
//        InputStream resourceAsStream = (InputStream) path
        byte[] bytes = IoUtil.readBytes(resourceAsStream);
//
        AnimalUtils.animalCheck(bytes,null);
    }
}
